package com.engine4cloud.tdf.education.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.core.util.lambda.Actions;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.education.convert.EduResourceConvert;
import com.engine4cloud.tdf.education.dto.create.EduResourceCreateDTO;
import com.engine4cloud.tdf.education.dto.query.EduResourceFileQueryDTO;
import com.engine4cloud.tdf.education.dto.query.EduResourceQueryDTO;
import com.engine4cloud.tdf.education.dto.result.EduResourceResultDTO;
import com.engine4cloud.tdf.education.dto.update.EduResourceUpdateDTO;
import com.engine4cloud.tdf.education.entity.EduResourceEntity;
import com.engine4cloud.tdf.education.mapper.EduResourceMapper;
import com.engine4cloud.tdf.education.service.EduResourceFileService;
import com.engine4cloud.tdf.education.service.EduResourceService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * 学习资源表
 *
 * @author wenjie
 * @date 2024-07-30 14:58:22
 */
@Service
public class EduResourceServiceImpl extends ServiceImpl<EduResourceMapper, EduResourceEntity> implements EduResourceService {

    @Resource
    private EduResourceFileService fileService;

    @Override
    public PageInfo<EduResourceResultDTO> pageList(Params pageable, EduResourceQueryDTO dto) {
        return Pages.convert(this.baseMapper.pageList(Pages.page(pageable), dto));
    }

    @Override
    public EduResourceResultDTO getById(Long id) {
        EduResourceResultDTO result = EduResourceConvert.INSTANCE.convert(super.getById(id));
        //设置文件
        result.setFileList(fileService.findList(new EduResourceFileQueryDTO(result.getCode())));
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean create(EduResourceCreateDTO dto) {
        TdfUser user = SecurityUtils.getUser();
        EduResourceEntity entity = EduResourceConvert.INSTANCE.create(dto, user.getTenantId());
        super.save(entity);
        //设置文件
        Actions.apply(CollectionUtils.isNotEmpty(dto.getFileList()), () -> fileService.batchSave(new EduResourceFileQueryDTO(entity.getCode()), dto.getFileList(), user));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(EduResourceUpdateDTO dto) {
        EduResourceEntity old = Optional.ofNullable(this.baseMapper.selectById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));
        TdfUser user = SecurityUtils.getUser();

        super.updateById(EduResourceConvert.INSTANCE.update(dto));
        Actions.apply(CollectionUtils.isNotEmpty(dto.getFileList()), () -> fileService.batchUpdate(new EduResourceFileQueryDTO(old.getCode()), dto.getFileList(), user));
        return true;
    }

    @Override
    public List<EduResourceResultDTO> findListByCourse(String courseCode) {
        return this.baseMapper.findListByCourse(courseCode);
    }
}